package red.mlz.module.config;

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class DataSourceAspect {
    @Pointcut("execution(* red.mlz.module.module..mapper.*.*(..))")
    public void dataSourcePointcut() {}

    @Before("dataSourcePointcut()")
    public void before() {
        String methodName = new Throwable().getStackTrace()[1].getMethodName();
        if (methodName.startsWith("select")) {
            DynamicDataSourceContextHolder.setDataSourceType("SLAVE");
        } else if (methodName.startsWith("insert") || methodName.startsWith("update") || methodName.startsWith("delete")) {
            DynamicDataSourceContextHolder.setDataSourceType("MASTER");
        }
    }
}